from flask import Flask,jsonify,render_template,request,escape
from logincheck import database
from logincheck import panduan
from api import geocode,around,weather
from api import geocode,walking
from api import geocode,driving
import pandas as pd
gaode_key = "31e2e9ede61f2f81638ca15063de2984"

app = Flask(__name__)

# 调用日志函数
def log_request(req:"flask_request",res:str) -> None:
	with open("vsearch.log","a") as log:
		print(req.form,req.remote_addr,req.user_agent,res,file=log,sep='|')

# 初始界面
@app.route('/',methods=['GET'])
def entry() ->'html':
	return render_template('entry.html',the_title='欢迎来到周边游攻略小网站！')

# 登陆界面
@app.route('/login',methods=['GET'])
def login() ->'html':
	return render_template('login.html',the_title='欢迎来到周边游攻略小网站的登录页面！')

# 注册界面
@app.route('/zhuce',methods=['GET'])
def zhuce() ->'html':
	return render_template('zhuce.html',the_title='欢迎来到周边游攻略小网站的注册页面！')

# 注册成功界面
@app.route('/success',methods=['POST'])
def success() ->'html':
	zhuce_zhanghao = request.form.get("zhuce-zhanghao") 
	zhuce_mima = request.form.get("zhuce-mima")
	database(zhuce_zhanghao,zhuce_mima)
	return render_template('success.html',the_title='欢迎成为周边游攻略小网站的用户！')

# 登录账号密码检查
@app.route('/logincheck',methods=['POST'])
def logincheck() ->'html':
	zhanghao = request.form.get("zhanghao") 
	mima = request.form.get("mima")
	zhanghao=int(zhanghao)
	mima=int(mima)
	a = panduan(zhanghao,mima)
	log_request(request,a)
	if a == "ture":
		return render_template('index.html',the_title ="欢迎来到周边游攻略小网站！")
	else:
		return render_template('login_error.html',the_title ="登录失败")

# 首页界面
@app.route('/index')
def index() ->'html':
	return render_template('index.html',the_title='欢迎来到周边游攻略小网站！')

# 跳转至周边查询界面
@app.route('/surrounding')
def surrounding() ->'html':
	return render_template('surrounding.html',the_title='欢迎来到周边查询页面！')

# 跳转至周边查询结果界面
@app.route('/surrounding_back',methods=['POST'])
def surrounding_back() ->'html':
	address = request.form.get('address')
	location = str(geocode(gaode_key,address))
	keyword = request.form.get('keywords')
	results = around(gaode_key,location,keyword)['pois']

	names = []
	address1 = []
	for i in results:
		names.append(i['name'])
		address1.append(i['address'])
	union = list(zip(names,address1))
	return render_template('surrounding_back.html',res=union,the_title='为您返回的周边查询结果：')


# 跳转至路径规划界面
@app.route('/route')
def route() ->'html':
	return render_template('route.html',the_title='欢迎来到路径规划页面！')

# 跳转至步行路径规划界面
@app.route('/walking')
def buxing() ->'html':
	return render_template('walking.html',the_title='欢迎来到步行路径规划页面！')

# 跳转至步行路径规划结果界面
@app.route('/walking_back',methods=['POST'])
def walking_back() ->'html':
	gaode_key = "31e2e9ede61f2f81638ca15063de2984"
	starting = request.form.get('starting')
	ending = request.form.get('ending')
	origin = str(geocode(gaode_key,starting))
	destination = str(geocode(gaode_key,ending))
	results = walking(gaode_key,origin,destination)
	return render_template('walking_back.html',res=results,the_title='为您返回的步行路径规划查询结果：')

# 跳转至驾车路径规划界面
@app.route('/driving')
def drive() ->'html':
	return render_template('driving.html',the_title='欢迎来到驾车路径规划页面！')


# 跳转至驾车路径规划结果界面
@app.route('/driving_back',methods=['POST'])
def driving_back() ->'html':
	gaode_key = "31e2e9ede61f2f81638ca15063de2984"
	starting = request.form.get('starting')
	ending = request.form.get('ending')
	origin = str(geocode(gaode_key,starting))
	destination = str(geocode(gaode_key,ending))
	results = driving(gaode_key,origin,destination)
	return render_template('driving_back.html',res=results,the_title='为您返回的驾车路径规划查询结果：')


# 跳转至天气查询界面
@app.route('/weather')
def tianqi() ->'html':
	return render_template('weather.html',the_title='欢迎来到天气查询页面！')

# 跳转至天气查询结果界面
@app.route('/weather_back',methods=['POST'])
def weather_back() -> 'html':
	maincity = request.form['city']
	tianqi = weather(maincity)
	province1 = tianqi["lives"][0]['province']
	city1 = tianqi["lives"][0]['city']
	tq1 = tianqi["lives"][0]['weather']
	temperature1 = tianqi["lives"][0]['temperature']
	winddirection1 = tianqi["lives"][0]['winddirection']
	windpower1 = tianqi["lives"][0]['windpower']
	humidity1 = tianqi["lives"][0]['humidity']
	reporttime1 = tianqi["lives"][0]['reporttime']
	return render_template('weather_back.html',the_title='为您返回的天气查询结果：',the_city = tianqi,
			province = province1,
			city = city1,
			weather = tq1,
			temperature = temperature1,
			winddirection = winddirection1,
			windpower = windpower1,
			humidity = humidity1,
			reporttime = reporttime1)


# 日志界面
@app.route('/viewlog')
def viewlog() ->'html':
	with open("vsearch.log",'r') as log:
		contents = []
		for line in log:
			contents.append([])
			for item in line.split('|'):
				contents[-1].append(escape(item))
	titles = ["Form data","Remote_addr","User_agent","Result"]
	return render_template('viewlog.html',the_data = contents,the_title = titles)

if __name__ == '__main__':
	app.run(debug = True)